package com.example.content.manager

import android.content.Context
import com.example.content.delegates.*
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMOptions
import com.hyphenate.easeui.EaseUI
import com.hyphenate.easeui.delegate.*
import com.hyphenate.easeui.manager.EaseChatPresenter
import com.hyphenate.easeui.manager.EaseConTypeSetManager

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/14
 */
object IMMgr {
    var isSDKInit = false

    fun init(context: Context) {
        if (initSDK(context)) {
            EMClient.getInstance().setDebugMode(true)
            initEaseUI(context)
            registerConversationType()
        }
    }

    /**
     * ChatPresenter中添加了网络连接状态监听，多端登录监听，群组监听，联系人监听，聊天室监听
     */
    private fun initEaseUI(context: Context) {
        //添加ChatPresenter,ChatPresenter中添加了网络连接状态监听
        // TODO: 2020/11/14 后续添加各种监听事件
        EaseUI.getInstance().addChatPresenter(EaseChatPresenter())
//        EaseUI.getInstance().settingsProvider
    }

    /**
     * 注册对话类型
     */
    private fun registerConversationType() {
        EaseConTypeSetManager.getInstance()
            .addConversationType(EaseExpressionAdapterDelegate::class.java) //自定义表情
            .addConversationType(EaseFileAdapterDelegate::class.java) //文件
            .addConversationType(EaseImageAdapterDelegate::class.java) //图片
            .addConversationType(EaseLocationAdapterDelegate::class.java) //定位
            .addConversationType(EaseVideoAdapterDelegate::class.java) //视频
            .addConversationType(EaseVoiceAdapterDelegate::class.java) //声音
            .addConversationType(ChatConferenceInviteAdapterDelegate::class.java) //会议邀请
            .addConversationType(ChatLiveInviteAdapterDelegate::class.java) //语音邀请
            .addConversationType(ChatRecallAdapterDelegate::class.java) //消息撤回
            .addConversationType(ChatVideoCallAdapterDelegate::class.java) //视频通话
            .addConversationType(ChatVoiceCallAdapterDelegate::class.java) //语音通话
            .addConversationType(EaseCustomAdapterDelegate::class.java) //自定义消息
            .addConversationType(ChatNotificationAdapterDelegate::class.java) //入群等通知消息
            .setDefaultConversionType(EaseTextAdapterDelegate::class.java) //文本
    }

    private fun initSDK(context: Context): Boolean {
        val options = initChatOptions(context)
        isSDKInit = EaseUI.getInstance().init(context, options)
        return isSDKInit
    }

    private fun initChatOptions(context: Context): EMOptions {
        val options = EMOptions()
        // 设置是否自动接受加好友邀请,默认是true
        options.acceptInvitationAlways = false
        // 设置是否需要接受方已读确认
        options.requireAck = true
        // 设置是否需要接受方送达确认,默认false
        options.requireDeliveryAck = false

        options.useRtcConfig = true
        return options
    }
}